//
//  NSObject+CreateControls.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/7.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "NSObject+CreateControls.h"
#import "UILabel+SuggestSize.h"

@implementation NSObject (CreateControls)

#pragma mark - Create Button

+ (UIButton *)createButtonBackgroudImage:(NSString *)image
                          imageHighlight:(NSString *)imageHighlight
                                   title:(NSString *)title
                          titleHighlight:(NSString *)titleHighlight {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.backgroundColor = CLEAR_COLOR;
    button.clipsToBounds = YES;
    if (image && imageHighlight) {
        [button setBackgroundImage:CREATE_IMG(image ? image : @"") forState:UIControlStateNormal];
        [button setBackgroundImage:CREATE_IMG(imageHighlight ? imageHighlight : @"") forState:UIControlStateHighlighted];
    }
    
    [button setTitle:title ? title : @"" forState:UIControlStateNormal];
    [button setTitle:titleHighlight ? titleHighlight : @"" forState:UIControlStateHighlighted];
    
    return button;
}


+ (UIButton *)createButtonBGImage:(UIImage *)image
                           hImage:(UIImage *)hImage
                            title:(NSString *)title
                           hTitle:(NSString *)hTitle {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.backgroundColor = CLEAR_COLOR;
    button.clipsToBounds = YES;
    if (image) {
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
    
    if (hImage) {
        [button setBackgroundImage:hImage forState:UIControlStateHighlighted];
    }
    [button setTitle:title ? title : @"" forState:UIControlStateNormal];
    [button setTitle:hTitle ? hTitle : @"" forState:UIControlStateHighlighted];
    
    return button;
}


+ (UIButton *)createButtonTarger:(SEL)targer
                           image:(UIImage *)image
                          hImage:(UIImage *)hImage
                           title:(NSString *)title
                          hTitle:(NSString *)hTitle {
    
    UIButton *button = [NSObject createButtonBGImage:image
                                              hImage:hImage
                                               title:title
                                              hTitle:hTitle];
    [button addTarget:self action:targer forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


#pragma mark - Create UIImageView

+ (UIImageView *)createImageView:(NSString *)image
                       highImage:(NSString *)highImage
                   clipsToBounds:(BOOL)clipsToBounds
                         vcModel:(UIViewContentMode)vcModel {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    if (image && highImage) {
        imageView.image = CREATE_IMG(image);
        imageView.highlightedImage = CREATE_IMG(highImage);
    }
    imageView.clipsToBounds = clipsToBounds;
    imageView.backgroundColor = CLEAR_COLOR;
    [imageView setContentMode:vcModel];
    
    return imageView;
}


+ (UIImageView *)createImgWithImage:(UIImage *)image
                             hImage:(UIImage *)hImage
                      clipsToBounds:(BOOL)clipsToBounds
                            vcModel:(UIViewContentMode)vcModel {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    if (image && hImage) {
        imageView.image = image;
        imageView.highlightedImage = hImage;
    }
    imageView.clipsToBounds = clipsToBounds;
    imageView.backgroundColor = CLEAR_COLOR;
    [imageView setContentMode:vcModel];
    
    return imageView;
    
}


#pragma mark - Create Label

+ (UILabel *)createLabelWithText:(NSString *)text
                       textColor:(UIColor *)textColor
                         bkColor:(UIColor *)bkColor
                            font:(UIFont *)font
                   textAlignment:(NSTextAlignment)textAlignment {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.backgroundColor = bkColor;
    label.font = font;
    label.textColor = textColor;
    label.textAlignment = textAlignment;
    
    return label;
}

+ (UILabel *)createNewlineLabelWithText:(NSString *)text
                              textColor:(UIColor *)textColor
                                bkColor:(UIColor *)bkColor
                                   font:(UIFont *)font
                          textAlignment:(NSTextAlignment)textAlignment {
    
    UILabel * label = [NSObject createLabelWithText:text
                                          textColor:textColor
                                            bkColor:bkColor
                                               font:font
                                      textAlignment:textAlignment];
    
    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.numberOfLines = 0;
    
    return label;
}

+ (UILabel *)createAdaptiveLabelWithText:(NSString *)text
                               textColor:(UIColor *)textColor
                                 bkColor:(UIColor *)bkColor
                                    font:(UIFont *)font
                                priority:(UILayoutPriority)priority
                                 forAxis:(UILayoutConstraintAxis)axis
                 preferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth
                           textAlignment:(NSTextAlignment)textAlignment {
    
    UILabel * label = [NSObject createNewlineLabelWithText:text
                                                 textColor:textColor
                                                   bkColor:bkColor
                                                      font:font
                                             textAlignment:textAlignment];
    
    [label setContentHuggingPriority:priority forAxis:axis];
    label.preferredMaxLayoutWidth = preferredMaxLayoutWidth;
    
    return label;
}

+ (UILabel *)createAdaptiveLabelWithText:(NSString *)text
                               textColor:(UIColor *)textColor
                                 bkColor:(UIColor *)bkColor
                                    font:(UIFont *)font
                 preferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth
                           textAlignment:(NSTextAlignment)textAlignment
{
    UILabel * label = [NSObject createAdaptiveLabelWithText:text
                                                  textColor:textColor
                                                    bkColor:bkColor
                                                       font:font
                                                   priority:UILayoutPriorityRequired
                                                    forAxis:UILayoutConstraintAxisVertical
                                    preferredMaxLayoutWidth:preferredMaxLayoutWidth
                                              textAlignment:textAlignment];
    return label;
}

+ (UILabel *)createAdaptiveLabelWithLengthText:(NSString *)text
                                         point:(CGPoint)point
                                     labelFont:(CGFloat)font
                                    lableWidth:(CGFloat)labelWidth {
    
    UILabel *label = [NSObject createNewlineLabelWithText:text
                                                textColor:[UIColor blackColor]
                                                  bkColor:[UIColor clearColor]
                                                     font:[UIFont systemFontOfSize:font]
                                            textAlignment:NSTextAlignmentLeft];
    
    CGSize size = [label suggestedSizeForWidth:labelWidth];
    label.frame = CGRectMake(point.x ? point.x : 0, point.y ? point.y : 0, size.width, size.height);
    
    return label;
}


#pragma mark - Create TextView

+ (UITextView *)createTextViewWithBorderWidth:(CGFloat)borderWidth
                                  borderColor:(UIColor *)borderColor
                                textAlignment:(NSTextAlignment)textAlignment
                                         font:(UIFont *)font
                                returnKeyType:(UIReturnKeyType)returnKeyType
                                    textColor:(UIColor *)textColor
                                      bkColor:(UIColor *)bkColor
                                 keyboardType:(UIKeyboardType)keyboardType {
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
    textView.layer.borderWidth = borderWidth;
    textView.layer.borderColor = CLEAR_COLOR.CGColor;
    textView.textAlignment = textAlignment;
    textView.font = font;
    textView.returnKeyType = returnKeyType;
    textView.keyboardType = keyboardType;
    textView.textColor = textColor;
    textView.backgroundColor = bkColor;
    
    return textView;
}


#pragma mark - Create TextField

+ (UITextField *)createTextFieldWithBorderWidth:(CGFloat)borderWidth
                                  textAlignment:(NSTextAlignment)textAlignment
                                  returnKeyType:(UIReturnKeyType)returnKeyType
                                      textColor:(UIColor *)textColor
                                backgroundColor:(UIColor *)backgroundColor
                                           font:(UIFont *)font
                                   keyboardType:(UIKeyboardType)keyboardType {
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero];
    
    textField.layer.borderWidth = borderWidth;
    textField.layer.borderColor= CLEAR_COLOR.CGColor;
    textField.textAlignment = textAlignment;
    textField.font = font;
    textField.returnKeyType = returnKeyType;
    textField.keyboardType = keyboardType;
    textField.textColor = textColor;
    textField.backgroundColor = backgroundColor;
    textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    textField.keyboardType = UIKeyboardTypeDefault;
    return textField;
}



#pragma mark - Create View

+ (UIView *)createViewWithBgColor:(UIColor *)bgColor {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    
    view.backgroundColor = bgColor ? bgColor : CLEAR_COLOR;
    view.clipsToBounds = YES;
    return view;
}


#pragma mark - Create ScrollView

+ (UIScrollView *)createScrollViewWithContentOffset:(CGPoint)contentOffset withContentSize:(CGSize)contentSize canPagingEnabled:(BOOL)pagingEnabled canScrollEnabled:(BOOL)scrollEnabled canBounces:(BOOL)bounces delegate:(id<UIScrollViewDelegate>)delegate {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.delegate = delegate;
    scrollView.contentSize = contentSize;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentOffset = contentOffset;
    
    scrollView.pagingEnabled = pagingEnabled;//整页翻动
    scrollView.scrollEnabled = scrollEnabled;//滚动
    scrollView.bounces = bounces;//控制控件遇到边框是否反弹
    //隐藏滚动条
    scrollView.showsVerticalScrollIndicator = FALSE;
    scrollView.showsHorizontalScrollIndicator = FALSE;
    
    return scrollView;
}


#pragma mark - Create Switch 

+ (UISwitch *)createSwitchWithIsOn:(BOOL)isOn {
    
    UISwitch *switch_  = [[UISwitch alloc] initWithFrame:CGRectZero];
    switch_.on = isOn;
    
    return switch_;
}


#pragma mark - Create TableView

+ (UITableView *)createTabelViewForFrame:(CGRect)frame andStyle:(UITableViewStyle)style delegate:(id<UITableViewDelegate>)delegate dataSource:(id<UITableViewDataSource>)dataSource canClipsToBounds:(BOOL)clipsToBounds canScrollEnabled:(BOOL)scrollEnabled isHideLine:(BOOL)hidLine withTableFootView:(UIView *)tableFooterView {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:style];
    tableView.backgroundColor = CLEAR_COLOR;
    tableView.delegate = delegate;
    tableView.dataSource = dataSource;
    tableView.clipsToBounds = clipsToBounds;
    tableView.scrollEnabled = scrollEnabled;
    tableView.estimatedRowHeight = 44.0;
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (hidLine) {
        //去掉单元格的分割线
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }

    tableView.tableFooterView = tableFooterView;
    return tableView;
}


#pragma mark - Create CollectionView

+ (UICollectionView *)createCollectionViewForFrame:(CGRect)frame withCollectionViewLayout:(UICollectionViewLayout *)flowLayout dataSource:(id<UICollectionViewDataSource>)dataSource delegate:(id<UICollectionViewDelegate>)delegate canScrollEnabled:(BOOL)scrollEnabled showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator bounces:(BOOL)bounces alwaysBounceVertical:(BOOL)alwaysBounceVertical {
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    
    collectionView.dataSource = dataSource;
    collectionView.delegate = delegate;
    
    collectionView.scrollEnabled = scrollEnabled;
    collectionView.backgroundColor = CLEAR_COLOR;
    collectionView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
    collectionView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
    collectionView.bounces = bounces;
    collectionView.alwaysBounceVertical = alwaysBounceVertical;

    return collectionView;
}


+ (UICollectionViewFlowLayout *)createFlowLayoutForSectionInset:(UIEdgeInsets)sectionInset withMinimumLineSpacing:(CGFloat)minimumLineSpacing minimumInteritemSpacing:(CGFloat)minimumInteritemSpacing itemSize:(CGSize)itemSize andHeaderReferenceSize:(CGSize)headerReferenceSize footerReferenceSize:(CGSize)footerReferenceSize scrollDirection:(UICollectionViewScrollDirection)direction {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:direction];                                          //设置其布局方向
    flowLayout.sectionInset = sectionInset;                                             //设置其边界
    flowLayout.itemSize = itemSize;                                                     //设置每个cell的size
    flowLayout.minimumLineSpacing = minimumLineSpacing;
    flowLayout.minimumInteritemSpacing = minimumInteritemSpacing;
    flowLayout.headerReferenceSize = headerReferenceSize;
    flowLayout.footerReferenceSize = footerReferenceSize;
    
    return flowLayout;
}


+ (UISlider *)createSliderWithValue:(CGFloat)value minimumValue:(CGFloat)minimumValue maximumValue:(CGFloat)maximumValue {
    
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectZero];
    slider.value = value;
    slider.minimumValue = minimumValue;
    slider.maximumValue = maximumValue;
    
    return slider;
}


@end
